use Action;

package MoveAction;
@ISA = qw/Action/;

sub init{
    my $self = shift;
    $self->dependencies(qw//);
}


sub perform{
    my $self=shift;
    
    # Initial validations
    my $result = $self->{OperationManager}->runOperations($self->params, qw/ValidateActingEntity ValidateMap ValidateDirection/);
    return $result unless($result->is("SUCCESS"));
    
    # Extract the position from the entity to use it in the calculation of the new one
    my $map = $self->params->{MAP};
    my $entity = $self->params->{ENTITY};

    my @pos = @{$entity->pos($map)};
    $self->params->{POS} = \@pos;

    # Finishing the action
    $result = $self->{OperationManager}->runOperations($self->params, qw/AlterPosition CheckCellOccupied/);
    
    if($result->is("CELL_OCCUPIED")){
	my $pos = $self->params->{POS};
	my $map = $self->params->{MAP};
	my $cell = $map->tiles->[$pos->[0]][$pos->[1]];
	
	my $creatures = $cell->contents->category("CREATURE");

	my $params = {};
	foreach my $creature(@{$creatures}){
	    $params->{ENTITY} = $creature;
	    $params->{MAP} = $map;
	    $self->{OperationManager}->runOperations($params, qw/KillEntity/);
	}

    }else{
	$result = $self->{OperationManager}->runOperations($self->params, qw/MoveEntity/);
    }

    return $result;
}

1;
